package bt

import (
	common "app/util"
	"errors"
	"sync"
)

type Session struct{
	infohashtable 	common.SMap
	hashlen 		int32
	lock 			sync.RWMutex
}

func NewSession() *Session {

	p := new(Session)
	p.hashlen = 0

	return p
}

func (p *Session) GetTorrent(key string ) (*BtSession,error) {

	if !p.infohashtable.IsValue(key) {
		bt := newBtSession(key)
		p.infohashtable.SetValue(key,bt)

		p.lock.Lock()
		p.hashlen = p.hashlen + 1;
		p.lock.Unlock()

		return bt,nil
	}else {
		bt,err := p.infohashtable.GetValue(key)
		if err != nil {
			return nil,errors.New("GetTorrent No Found")
		}

		if value,ok := bt.(*BtSession) ; ok{
			return value,nil
		}
		return nil,errors.New("GetTorrent value No Found")

	}
	return nil,errors.New("no found ")
}
func (p *Session) GetTorrentCount() int32 {

	return p.hashlen
}
func (p *Session) DelTorrent(key string )  {
	p.infohashtable.DeleteValue(key)
}




